package com.guigu.www.service.imp;

import  com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.www.mapper.RolesMapper;
import com.guigu.www.mapper.UserRolesMapper;
import com.guigu.www.mapper.UsersMapper;
import com.guigu.www.pojo.Roles;
import com.guigu.www.pojo.UserRole;
import com.guigu.www.pojo.Users;
import com.guigu.www.service.UserRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserRolesServiceImpl extends ServiceImpl<UserRolesMapper,UserRole> implements UserRolesService {
    @Autowired
    UserRolesMapper mapper;
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    RolesMapper rolesMapper;
    @Override
    public void addUserRole(Users users) {
        mapper.deleteUserRolesbyUserid(users.getId());
        for(Roles roles : users.getRoles()){
            UserRole userRole=new UserRole();
            userRole.setUserid(users.getId());
            userRole.setRoleid(roles.getId());
            mapper.addUserRole(userRole);
        }
    }

    @Override
    public List<Roles> addUserRoles(int userid) {
        //删除当前用户的角色
        List<Roles> roles=rolesMapper.queryall();
        List<Integer> list=rolesMapper.querybyuserid(userid);
        for(Roles role : roles){
                if (list.contains(role.getId())){
                    role.setCheck(true);
            }
        }
        return roles;
    }
}
